/**
 * 字符串使用占位符轻松实现拼接
 * var str1 = "hello {0}".format("world"); //log   hello world
 * var str1 = "我叫{0},性别{1}".format("美男子", "男"); //log 我叫美男子,性别男
 * var user = {name: "美男子",sex: "男",age: 20};
 * var str2 = "我叫{name},性别{sex},今年{age}岁".format(user); //我叫美男子,性别男,今年20岁
 * @returns {String}
 */
String.prototype.formatStr = function () {
    if (arguments.length == 0) return this;
    var param = arguments[0];
    var s = this;
    if (typeof(param) == 'object') {
        for (var key in param)
            s = s.replace(new RegExp("\\{" + key + "\\}", "g"), param[key]);
        return s;
    } else {
        for (var i = 0; i < arguments.length; i++)
            s = s.replace(new RegExp("\\{" + i + "\\}", "g"), arguments[i]);
        return s;
    }
};

/**
 * 拖动div插件 主要适用在漂浮在地图上的div面板拖动
 * $('.mainDiv .mainDiv-title').dragDiv(".mainDiv");
 */
(function ($) {
    $.fn.dragDiv = function (divWrap) {
        return this.each(function () {
            var $divMove = $(this);//鼠标可拖拽区域
            $(this).attr("title","按住可拖拽面板");
            var $divWrap = divWrap ? $divMove.parents(divWrap) : $divMove;//整个移动区域
            var mX = 0, mY = 0;//定义鼠标X轴Y轴
            var dX = 0, dY = 0;//定义div左、上位置
            var isDown = false;//mousedown标记
            if (document.attachEvent) {//ie的事件监听，拖拽div时禁止选中内容，firefox与chrome已在css中设置过-moz-user-select: none; -webkit-user-select: none;
                $divMove[0].attachEvent('onselectstart', function () {
                    return false;
                });
            }
            $divMove.mousedown(function (event) {
                $(this).css("cursor", "move");//改变鼠标指针的形状
                var event = event || window.event;
                mX = event.clientX;
                mY = event.clientY;
                dX = $divWrap.offset().left;
                dY = $divWrap.offset().top;
                isDown = true;//鼠标拖拽启动
            });
            $(document).mousemove(function (event) {
                var event = event || window.event;
                var x = event.clientX;//鼠标滑动时的X轴
                var y = event.clientY;//鼠标滑动时的Y轴
                if (isDown) {
                    $divWrap.css({"left": x - mX + dX, "top": y - mY + dY});//div动态位置赋值
                }
            });
            $divMove.mouseup(function () {
                // $(this).css("cursor", "default");
                isDown = false;//鼠标拖拽结束
            });
        });
    };
})(jQuery);